#include "../include/web_lib.hpp"

namespace gcj
{

void WebLib::loadLibrary() {
    ifstream *ifs4offset = new ifstream("../data/newOffset.dat");
    ifstream * ifs4pagelib = new ifstream("../data/newRipePage.dat");
    ifstream * ifs4invertIdx = new ifstream("../data/invertindex.dat");

    string offsetTmp;
    stringstream offset;
    long docid = 0;
    long beg = 0;
    int len = 0;
    long cnt = 0;    //网页总数

    //加载偏移库，并根据偏移库数据加载网页库，保存在容器中
    while(getline(*ifs4offset, offsetTmp, '\n')) {
        offset.str(offsetTmp);
        offset >> docid >> beg >> len;
        _offsetLib[docid] = {beg, len};

        //读取网页库中的内容
        char *pageChTmp = (char *)calloc(len + 1, sizeof(char));
        ifs4pagelib->read(pageChTmp, len);
        pageChTmp[len] = '\0';
        string pageStr(pageChTmp);

        shared_ptr<WebPage> wpTmp(new WebPage(pageStr));
        _pageLib[docid] = wpTmp;

        //回收一下资源
        offsetTmp.clear();
        offset.clear();
        free(pageChTmp);
        pageChTmp = nullptr;
        ++cnt;
    } 
    //读取倒排索引
    string idxTmp;
    char ctmp[64] = {0};
    stringstream idxss;
    while(getline(*ifs4invertIdx, idxTmp, '\n')) {
        string word;
        long docid = 0;
        double weight;
        idxss.str(idxTmp);
        idxss >> ctmp;  //需要先写到c风格字符串中
        word = ctmp;
        while(idxss >> docid >> weight) {
            //读取词语出现过的页面的id，以及该词语在那一页的权重，保存在容器中
            _invertIndexTable[word].insert({docid, weight});
        }
        idxss.clear();
    }

    //关闭文件
    ifs4offset->close();
    ifs4pagelib->close();
    ifs4invertIdx->close();
    delete ifs4offset;
    delete ifs4pagelib;
    delete ifs4invertIdx;
    ifs4offset = nullptr;
    ifs4pagelib = nullptr;
    ifs4invertIdx = nullptr;
}

}//end of namespace gcj
